package com.xiaomazi.video.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.req.UploadVideoRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyun.vod.upload.resp.UploadVideoResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.xiaomazi.common.utils.R;
import com.xiaomazi.servicebase.exception.LLXYException;
import com.xiaomazi.video.config.VideoPropertiesEntityConfig;
import com.xiaomazi.video.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: llxy-parent
 * @description: 视频业务实现类
 * @author: 小马子
 * @create: 2023-02-19 20:18
 **/
@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VideoPropertiesEntityConfig videoPropertiesEntityConfig;

    @Autowired
    private DefaultAcsClient client;

    /**
     * 上传视频
     * @param file 视频文件
     * @return 视频id
     */
    @Override
    public R uploadAlyiVideo(MultipartFile file) {
        try {
            String fileName = file.getOriginalFilename();
            String title = fileName.substring(0,fileName.lastIndexOf("."));
            InputStream stream = file.getInputStream();

            UploadStreamRequest request = new UploadStreamRequest(videoPropertiesEntityConfig.getKeyId(),
                    videoPropertiesEntityConfig.getKeySecret(),
                    title,fileName,stream);
            UploadStreamResponse response = new UploadVideoImpl().uploadStream(request);
            String videoId = null;
            if (response.isSuccess()) {
                videoId = response.getVideoId();
            } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                videoId = response.getVideoId();
            }
            return R.ok().data("videoId",videoId);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 根据视频Id删除视频
     * @param id 视频id
     * @return 20000
     */
    @Override
    public R removeAlyVideo(String id) {
        try {
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(id);
            client.getAcsResponse(request);
            return R.ok();
        } catch (ClientException e) {
            throw new LLXYException(20001,"删除视频失败");
        }
    }

    /**
     * 根据视频Id集合删除视频
     * @param videoIdList 视频id集合
     * @return 20000
     */
    @Override
    public R removeMoreAlyVideo(List<String> videoIdList) {
        try {
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(videoIdList.stream().collect(Collectors.joining(",")));
            client.getAcsResponse(request);
            return R.ok();
        } catch (ClientException e) {
            throw new LLXYException(20001,"删除视频失败");
        }
    }

    /**
     * 根据视频id获取视频凭证
     * @param vid 视频id
     * @return 视频凭证字符串
     */
    @Override
    public R getPlayAuth(String vid) {
        try {
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(vid);
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            return R.ok().data("playAuth",response.getPlayAuth());
        } catch (ClientException e) {
            throw new RuntimeException(e);
        }
    }
}
